package com.shyfu.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Svitlana on 03-Apr-14.
 */
@Entity
@Table(name = "BOOKS", schema = "SHYFU", catalog = "")
public class Book {
    private long bookId;
    @NotNull
    @NotEmpty
    @Size(max = 100)
    private String title;
    @Size(max = 100)
    private String cover;
    @NotNull
    @NotEmpty
    @Size(max = 100)
    private String filepath;
    @Size(max = 4000)
    private String description;
    private Timestamp wrote;
    @NotNull
    private Timestamp created;
    private Author author;
    private User user;
    private List<Comment> comments;

    @Id
    @Column(name = "BOOK_ID", nullable = false, insertable = true, updatable = true, precision = 0)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_seq")
    @SequenceGenerator(name = "book_seq", sequenceName = "BOOKIDSEQUENCE", allocationSize = 1)
    public long getBookId() {
        return bookId;
    }

    public void setBookId(long bookId) {
        this.bookId = bookId;
    }

    @Basic
    @Column(name = "TITLE", nullable = false, insertable = true, updatable = true, length = 100)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Basic
    @Column(name = "COVER", nullable = true, insertable = true, updatable = true, length = 100)
    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    @Basic
    @Column(name = "FILEPATH", nullable = false, insertable = true, updatable = true, length = 100)
    public String getFilepath() {
        return filepath;
    }

    public void setFilepath(String filepath) {
        this.filepath = filepath;
    }

    @Basic
    @Column(name = "DESCRIPTION", nullable = true, insertable = true, updatable = true, length = 4000)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Basic
    @Column(name = "WROTE", nullable = true, insertable = true, updatable = true)
    public Timestamp getWrote() {
        return wrote;
    }

    public void setWrote(Timestamp wrote) {
        this.wrote = wrote;
    }

    @Basic
    @Column(name = "CREATED", nullable = false, insertable = true, updatable = true)
    public Timestamp getCreated() {
        return created;
    }

    public void setCreated(Timestamp created) {
        this.created = created;
    }

    @ManyToOne
    @JoinColumn(name = "AUTHOR_ID")
    @JsonIgnore
    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

    @ManyToOne
    @JoinColumn(name = "USER_ID")
    @JsonIgnore
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "BOOK_ID")
    @JsonIgnore
    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (bookId != book.bookId) return false;
        if (cover != null ? !cover.equals(book.cover) : book.cover != null) return false;
        if (created != null ? !created.equals(book.created) : book.created != null) return false;
        if (description != null ? !description.equals(book.description) : book.description != null) return false;
        if (filepath != null ? !filepath.equals(book.filepath) : book.filepath != null) return false;
        if (title != null ? !title.equals(book.title) : book.title != null) return false;
        if (wrote != null ? !wrote.equals(book.wrote) : book.wrote != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        long result = bookId;
        result = 31 * result + (title != null ? title.hashCode() : 0);
        result = 31 * result + (cover != null ? cover.hashCode() : 0);
        result = 31 * result + (filepath != null ? filepath.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (wrote != null ? wrote.hashCode() : 0);
        result = 31 * result + (created != null ? created.hashCode() : 0);
        return ((Long) result).intValue();
    }
}
